package cn.stu.system.controller;


import cn.stu.common.utils.ApiData;
import cn.stu.framework.entity.LoginLog;
import cn.stu.framework.service.LoginLogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/loginLog")
public class LoginLogController {
    @Autowired
    private LoginLogService sysLoginLogService;

    //分页查询部门数据
    @ApiOperation("分页获取数据")
    @GetMapping("/page/{currentPage}/{pageSize}")
    public ApiData page(@PathVariable Long currentPage,
                        @PathVariable Long pageSize) {
        IPage<LoginLog> pageInfo = new Page<>(currentPage, pageSize);
        QueryWrapper<LoginLog> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        sysLoginLogService.page(pageInfo, wrapper);
        return ApiData.getPageTrue("", pageInfo.getTotal(), pageInfo.getRecords());
    }

    @ApiOperation("新增日志")
    @PostMapping("/add")
    public ApiData add(@RequestBody LoginLog loginLog) {
        try {
            sysLoginLogService.save(loginLog);
            return ApiData.getTrue("");
        } catch (Exception e) {
            e.printStackTrace();
            return ApiData.getFalse("");
        }
    }

    @ApiOperation("修改部门数据")
    @PutMapping("/update")
    public ApiData update(@RequestBody LoginLog loginLog) {
        try {
            sysLoginLogService.updateById(loginLog);
            return ApiData.getTrue("");
        } catch (Exception e) {
            e.printStackTrace();
            return ApiData.getFalse("");
        }
    }

    @ApiOperation("删除部门数据")
    @DeleteMapping("/delete/{id}")
    public ApiData delete(@PathVariable Long id) {
        //根据id删除数据
        try {
            sysLoginLogService.removeById(id);
            return ApiData.getTrue("");
        } catch (Exception e) {
            e.printStackTrace();
            return ApiData.getFalse("");
        }
    }
}
